{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CSG Logical Operators\n",
    "This example creates a simple geometry and shows how the various logical operators work.\n",
    "\n",
    "First we will create two circles and halfspace regions that we will use in our examples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Load the Library\n",
    "using ConstructiveSolidGeometry\n",
    "\n",
    "left_circle  = InfCylinder(Coord(-0.25, 0.0, 0.0), unitize(Coord(0.0, 0.0, 1.0)), 0.5)\n",
    "right_circle = InfCylinder(Coord(0.25, 0.0, 0.0), unitize(Coord(0.0, 0.0, 1.0)), 0.5)\n",
    "\n",
    "regions = Array{Region}(0)\n",
    "push!(regions, Region(left_circle, -1))\n",
    "push!(regions, Region(right_circle, -1));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With these surfaces to use, we can experiment with combining them using different operators to form cells"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Union of two circles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"\" />"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "definition = :(1 | 2)\n",
    "\n",
    "cells = Array{Cell}(0)\n",
    "push!(cells, Cell(regions, definition));\n",
    "bounding_box = Box(Coord(-1.0, -1.0, -1.0), Coord(1.0, 1.0, 1.0))\n",
    "geometry = Geometry(cells, bounding_box)\n",
    "\n",
    "view = Box(Coord(-1.0, -1.0, 0), Coord(1.0, 1.0, 0))\n",
    "plot_cell_2D(geometry, view , 500, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Complement of the union of two circles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"\" />"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "definition = :(~(1 | 2))\n",
    "\n",
    "cells = Array{Cell}(0)\n",
    "push!(cells, Cell(regions, definition))\n",
    "geometry = Geometry(cells, bounding_box)\n",
    "\n",
    "plot_cell_2D(geometry, view, 500, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Intersection of two circles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"\" />"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "definition = :(1 ^ 2)\n",
    "\n",
    "cells = Array{Cell}(0)\n",
    "push!(cells, Cell(regions, definition))\n",
    "geometry = Geometry(cells, bounding_box)\n",
    "\n",
    "plot_cell_2D(geometry, view, 500, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Crescent Moon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"\" />"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "definition = :(~(1 ^ ~2))\n",
    "\n",
    "cells = Array{Cell}(0)\n",
    "push!(cells, Cell(regions, definition))\n",
    "geometry = Geometry(cells, bounding_box)\n",
    "\n",
    "plot_cell_2D(geometry, view, 500, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Circles Cancelling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"\" />"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "definition = :(~(1 ^ 2) ^ (1 | 2))\n",
    "\n",
    "cells = Array{Cell}(0)\n",
    "push!(cells, Cell(regions, definition))\n",
    "geometry = Geometry(cells, bounding_box)\n",
    "\n",
    "plot_cell_2D(geometry, view, 500, 1)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Julia 0.5.0",
   "language": "julia",
   "name": "julia-0.5"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
